"""
Base CLI command class for CodeMCP.
"""

import argparse
from typing import TYPE_CHECKING
from pathlib import Path

from ..core.error_handler import CodeMCPError

if TYPE_CHECKING:
    from .main import CodeMCPCLI


class CLICommand:
    """Base class for CLI commands."""
    
    def __init__(self, cli: 'CodeMCPCLI'):
        self.cli = cli
        self.config = cli.config
        self.formatter = cli.formatter
        self.printer = cli.printer
        self.progress = cli.progress
    
    def add_parser(self, subparsers) -> argparse.ArgumentParser:
        """Add command parser to subparsers. Must be implemented by subclasses."""
        raise NotImplementedError
    
    async def execute(self, args: argparse.Namespace) -> int:
        """Execute the command. Must be implemented by subclasses."""
        raise NotImplementedError
    
    def validate_file_path(self, file_path: str) -> Path:
        """Validate and return file path."""
        path = Path(file_path)
        if not path.exists():
            raise CodeMCPError(f"File not found: {file_path}")
        if not path.is_file():
            raise CodeMCPError(f"Path is not a file: {file_path}")
        return path
    
    def get_language_from_extension(self, file_path: Path) -> str:
        """Determine language from file extension."""
        extension_map = {
            '.py': 'python',
            '.cpp': 'cpp',
            '.cxx': 'cpp',
            '.cc': 'cpp',
            '.c': 'cpp',
            '.hpp': 'cpp',
            '.h': 'cpp',
            '.f90': 'fortran',
            '.f95': 'fortran',
            '.f03': 'fortran',
            '.f08': 'fortran',
            '.f': 'fortran',
            '.for': 'fortran',
            '.ts': 'typescript',
            '.tsx': 'typescript',
            '.js': 'typescript',
            '.jsx': 'typescript',
            '.tex': 'latex',
            '.md': 'markdown',
            '.markdown': 'markdown'
        }
        
        extension = file_path.suffix.lower()
        language = extension_map.get(extension)
        
        if not language:
            raise CodeMCPError(f"Unsupported file extension: {extension}")
        
        return language